// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// v [author] rhyme_lph
// P [email]  rhymelph@gmail.com
// k [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/widgets/button/normal_text_button.dart';
import 'package:finance_app/src/widgets/common_app_bar.dart';
import 'package:finance_app/src/widgets/input/search_input_field.dart';
import 'package:finance_app/src/widgets/list_item/list_header_item.dart';
import 'package:finance_app/src/widgets/select/normal_radio.dart';
import 'package:flutter/material.dart';
import 'package:finance_app/src/provider/all_reports/age/age_select_filter_provider.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';
import 'package:flutter_sticky_header/flutter_sticky_header.dart';
import 'package:tuple/tuple.dart';

@RouterMeta(paramName: "ageSelectFilterPage")
class AgeSelectFilterPage extends StatefulWidget {
  final List<String> filterList;
  final String select;

  const AgeSelectFilterPage({Key key, this.filterList, this.select})
      : super(key: key);

  @override
  _AgeSelectFilterPageState createState() => _AgeSelectFilterPageState();
}

class _AgeSelectFilterPageState extends State<AgeSelectFilterPage>
    with PagePresenter<AgeSelectFilterPage, AgeSelectFilterProvider> {
  @override
  // TODO: implement appBar
  PreferredSizeWidget get appBar =>
      getDefaultAppBar(title: S.current.filter, actions: [
        NormalResetTextButton(
          onTap: provider.onReset,
        ),
      ]);

  @override
  void initState() {
    provider.initList(widget.filterList);
    provider.currentSelect = widget.select;
    super.initState();
  }

  @override
  Widget get buildWidget => ColoredBox(
        color: Colors.white,
        child: Column(
          children: [
            AppSpaces.h20,
            SearchInputField(
              controller: provider.searchController,
              focusNode: provider.searchFocusNode,
            ),
            AppSpaces.h20,
            Expanded(
                child: listBuilder<Map<String, List<String>>>(
              selector: (_, __) => Map.from(__.groupMap),
              builder: (BuildContext context, Map<String, List<String>> value,
                  Widget child) {
                return value.isEmpty
                    ? super.buildEmptyWidget
                    : CustomScrollView(
                        slivers: buildSearchWidget(),
                      );
              },
            ))
          ],
        ),
      );

  List<Widget> buildSearchWidget() {
    return provider.groupMap.entries
        .toList()
        .map((e) => buildSliver(e.key, e.value))
        .toList();
  }

  Widget buildSliver(String title, List<String> data) => SliverStickyHeader(
        header: buildSliverHeader(title: title),
        sticky: false,
        sliver: SliverList(
          delegate: SliverChildBuilderDelegate(
            (context, i) => buildSliverItem(data[i]),
            childCount: data.length,
          ),
        ),
      );

  Widget buildSliverHeader({String title}) {
    final String _title = title.isEmpty ? "*" : title;
    return ListHeaderItem(
      title: _title,
    );
  }

  Widget buildSliverItem(String data) {
    return listBuilder<Tuple2<String, bool>>(
      selector: (_, __) => Tuple2(
        data,
        data == __.currentSelect,
      ),
      builder:
          (BuildContext context, Tuple2<String, bool> value, Widget child) =>
              PressedOpacityButton(
        onPressed: () => provider.onSelect(value.item1),
        padding: EdgeInsets.symmetric(
          vertical: getDp(10),
          horizontal: getDp(34),
        ),
        child: NormalRadio(
          title: value.item1 ?? '',
          isSelect: value.item2,
        ),
      ),
    );
  }
}
